package com.guangyunl.d_array;
// 二维数组的定义以及遍历
public class Demo03Array {
    public static void main(String[] args) {
        int[][] arr1 = new int[2][2];
        int[][] arr2 = new int[2][];

        System.out.println("========================");

        String[][] arr3 = {
                {"元素1"},
                {"元素1","元素2"},
                {"元素1","元素2","元素3"},
                {"元素1","元素2"}
        };

        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr1[i].length; j++) {
                System.out.print("arr1["+ i +"][" + j + "] = " + arr1[i][j]);
                System.out.print("  ");
            }
            System.out.println();
        }

        System.out.println("========================");

//        for (int i = 0; i < arr2.length; i++) {
//            for (int j = 0; j < arr2[i].length; j++) {
//                System.out.print("arr1["+ i +"][" + j + "] = " + arr2[i][j]);
//                System.out.print("  ");
//            }
//            System.out.println();
//        }
//28行 NullPointerException 因为 int[][] arr2 = new int[2][] 中未定义arr2的每一行的长度

        System.out.println("arr2[i].length -> NullPointerException");

        System.out.println("========================");

        for (int i = 0; i < arr3.length; i++) {
            for (int j = 0; j < arr3[i].length; j++) {
                System.out.print("arr1["+ i +"][" + j + "] = " + arr3[i][j]);
                System.out.print("  ");
            }
            System.out.println();
        }

        System.out.println("========================");

        System.out.println("arr1 = " + arr1);
        System.out.println("arr2 = " + arr2);
        System.out.println("arr3 = " + arr3);
    }
}


/*
========================
arr1[0][0] = 0  arr1[0][1] = 0
arr1[1][0] = 0  arr1[1][1] = 0
========================
arr2[i].length -> NullPointerException
========================
arr1[0][0] = 元素1
arr1[1][0] = 元素1  arr1[1][1] = 元素2
arr1[2][0] = 元素1  arr1[2][1] = 元素2  arr1[2][2] = 元素3
arr1[3][0] = 元素1  arr1[3][1] = 元素2
========================
arr1 = [[I@34a245ab
arr2 = [[I@7cc355be
arr3 = [[Ljava.lang.String;@12edcd21
 */